﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using AjaxControlToolkit;
using System.Threading;
using System.Collections.Generic;
using System.Collections.Specialized;
using NCCITPubSaleModel;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ProductService
{
    [OperationContract]
    public CascadingDropDownNameValue[] GetCategory(string knownCategoryValues, string category)
    {
        //Thread.Sleep(1000); // 等待1秒，模拟网络延迟

        using (NCCITPubSaleEntities ctx = new NCCITPubSaleEntities())
        {
            var ctylist = ctx.Category.ToList();

            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

            foreach (var cty in ctylist)
            {
                string id = cty.ID.ToString();
                string name = cty.Name;
                values.Add(new CascadingDropDownNameValue(name, id));
            }
            return values.ToArray();
        }
    }

    [OperationContract]
    public CascadingDropDownNameValue[] GetProduct(string knownCategoryValues, string category)
    {
        //Thread.Sleep(1000);

        StringDictionary keyValue =
            CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);

        int categoryId = Convert.ToInt32(keyValue["Category"]);

        using (NCCITPubSaleEntities ctx = new NCCITPubSaleEntities())
        {
            var products = ctx.Product.Where(p => p.Category.ID == categoryId).ToList();

            List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();

            foreach (var product in products)
            {
                string id = product.ID.ToString();
                string name = product.Name;
                values.Add(new CascadingDropDownNameValue(name, id));
            }

            return values.ToArray();
        }
    }

}
